package org.ring.common.transport;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

/**

 * @since 2024-07-19 18:37
 */
public class RpcCompletableFuture extends CompletableFuture {
    static final ConcurrentMap<Long, CompletableFuture<Message>> futures = new ConcurrentHashMap<>();

    private static final AtomicLong REQUEST_ID_GENERATOR = new AtomicLong();

    public static CompletableFuture newFuture(Message message){
        CompletableFuture future =  new CompletableFuture();
        long requestId = REQUEST_ID_GENERATOR.getAndIncrement();
        message.setId(requestId);
        futures.put(requestId,future);
        return future;
    }

    public static CompletableFuture removeFuture(Message message){
        return futures.remove(( message).getId());
    }
}
